/*
 * dbgcommon.cpp
 *
 *  Created on: 2013-11-7
 *      Author: WinDDK
 */

#include "dbgcommon.h"

POBJECT_TYPE DbgkDebugObjectType;
FAST_MUTEX DbgkpProcessDebugPortMutex;
PVOID KeUserExceptionDispatcher;
PUCHAR KeI386XMMIPresent;
BOOLEAN PsImageNotifyEnabled;
PULONG KeFeatureBits;
UNICODE_STRING PsNtDllPathName;
PVOID PsSystemDllBase;
VOID     (FASTCALL *HalRequestSoftwareInterrupt)(KIRQL RequestIrql);
VOID     (WINAPI *IopDeallocateApc)(PVOID P, int a2, int a3, int a4, int a5);
BOOLEAN  (WINAPI *KdIsThisAKdTrap)(IN PEXCEPTION_RECORD ExTrapFrameceptionRecord,IN PCONTEXT ContextRecord,IN KPROCESSOR_MODE PreviousMode);
VOID     (WINAPI *KeContextFromKframes)(IN PKTRAP_FRAME TrapFrame,PKEXCEPTION_FRAME ExceptionFrame,PCONTEXT ContextFrame);
VOID     (WINAPI *KeContextToKframes)( PKTRAP_FRAME TrapFrame,PKEXCEPTION_FRAME ExceptionFrame,IN PCONTEXT ContextFrame,IN ULONG ContextFlags,IN KPROCESSOR_MODE PreviousMode);
VOID     (WINAPI *KeFreezeAllThreads)(VOID);
VOID     (WINAPI *KeThawAllThreads)(VOID);
BOOLEAN  (WINAPI *KiCheckForAtlThunk)(IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT Context);
ULONG    (WINAPI *KiCopyInformation)(IN OUT PEXCEPTION_RECORD ExceptionRecord1, IN PEXCEPTION_RECORD ExceptionRecord2);
BOOLEAN  (WINAPI *KiDebugRoutine)(IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame, IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT ContextRecord, IN KPROCESSOR_MODE PreviousMode, IN BOOLEAN SecondChance);
VOID     (WINAPI *KiEspToTrapFrame)(IN PKTRAP_FRAME TrapFrame, IN ULONG Esp);
VOID     (WINAPI *KiSegSsToTrapFrame)(IN PKTRAP_FRAME, IN ULONG SegSs);
VOID     (WINAPI *KiSetSwapEvent)();
BOOLEAN  (WINAPI *KiSwapProcess)(IN PKPROCESS NewProcess,IN PKPROCESS OldProcess);
INT      (FASTCALL *KiSwapThread)();
VOID     (FASTCALL *KiUnlockDispatcherDatabase)(KIRQL irql);
VOID     (FASTCALL *KiReadyThread)(IN PKTHREAD Thread);
NTSTATUS (WINAPI *LpcRequestWaitReplyPortEx)(IN PVOID PortAddress, IN PPORT_MESSAGE RequestMessage, OUT PPORT_MESSAGE ReplyMessage);
NTSTATUS (FASTCALL *MiMakeProtectionMask)(unsigned int a1);
NTSTATUS (WINAPI *MiProtectVirtualMemory)(IN PEPROCESS Process,IN PVOID *BaseAddress,IN PULONG RegionSize,IN ULONG NewProtectWin32,IN PULONG LastProtect);
NTSTATUS (WINAPI *MmCopyVirtualMemory)(IN PEPROCESS FromProcess, IN CONST VOID *FromAddress, IN PEPROCESS ToProcess, OUT PVOID ToAddress, IN SIZE_T BufferSize, IN KPROCESSOR_MODE PreviousMode, OUT PSIZE_T NumberOfBytesCopied);
NTSTATUS (WINAPI *MmGetFileNameForAddress)(IN PVOID ProcessVa, OUT PUNICODE_STRING FileName);
NTSTATUS (WINAPI *MmGetFileNameForSection)(IN PVOID SectionObject,OUT POBJECT_NAME_INFORMATION *FileNameInfo);
VOID     (WINAPI *ObpDeleteObjectType )(IN PVOID Object);
NTSTATUS (WINAPI *ObDuplicateObject)(IN PEPROCESS SourceProcess, IN HANDLE SourceHandle, IN PEPROCESS TargetProcess OPTIONAL, OUT PHANDLE TargetHandle OPTIONAL, IN ACCESS_MASK DesiredAccess, IN ULONG HandleAttributes, IN ULONG Options, IN KPROCESSOR_MODE PreviousMode);
VOID     (WINAPI *PsCallImageNotifyRoutines)(IN PUNICODE_STRING FullImageName, IN HANDLE ProcessId, IN PIMAGE_INFO ImageInfo);
PEPROCESS(WINAPI *PsGetNextProcess)(IN PEPROCESS Process);
PETHREAD (WINAPI *PsGetNextProcessThread)(IN PEPROCESS Process, IN PETHREAD Thread);
VOID     (WINAPI *PsQuitNextProcessThread)(IN PETHREAD Thread);
NTSTATUS (WINAPI *PsResumeThread)(IN PETHREAD Thread, OUT PULONG PreviousSuspendCount OPTIONAL);
NTSTATUS (WINAPI *PsSuspendThread)(IN PETHREAD Thread, OUT PULONG PreviousSuspendCount);
NTSTATUS (WINAPI *PsTerminateProcess)(IN PEPROCESS Process, IN NTSTATUS Status);
NTSTATUS (WINAPI *PspCreateThread)(OUT PHANDLE ThreadHandle,IN ACCESS_MASK DesiredAccess,IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,IN HANDLE ProcessHandle,IN PEPROCESS ProcessPointer,OUT PCLIENT_ID ClientId OPTIONAL,IN PCONTEXT ThreadContext OPTIONAL,IN PINITIAL_TEB InitialTeb OPTIONAL,IN BOOLEAN CreateSuspended,IN PKSTART_ROUTINE StartRoutine OPTIONAL,IN PVOID StartContext);
BOOLEAN (WINAPI *RtlDispatchException)(IN PEXCEPTION_RECORD ExceptionRecord,IN PCONTEXT ContextRecord);

VOID __cdecl ProbeForWriteSmallStructure( IN PVOID Address, IN SIZE_T Size, IN ULONG Alignment)

/*++

 Routine Description:

 Probes a structure for write access whose size is known at compile time.

 Arguments:

 Address - Supples a pointer to the structure.

 Size - Supplies the size of the structure.

 Alignment - Supplies the alignment of structure.

 Return Value:

 None

 --*/

{

    ASSERT((Alignment == 1) || (Alignment == 2) || (Alignment == 4) || (Alignment == 8) || (Alignment == 16));

    //
    // If the size of the structure is > 4k then call the standard routine.
    // wow64 uses a page size of 4k even on ia64.
    //

    if ((Size == 0) || (Size >= 0x1000))
    {

        ASSERT(0);

        ProbeForWrite(Address, Size, Alignment);

    } else
    {
        if (((ULONG_PTR) (Address) & (Alignment - 1)) != 0)
        {
            ExRaiseDatatypeMisalignment();
        }

#if defined(_AMD64_)

        if ((ULONG_PTR)(Address) >= (ULONG_PTR)MM_USER_PROBE_ADDRESS)
        {
            Address = (UCHAR * const)MM_USER_PROBE_ADDRESS;
        }

        ((volatile UCHAR *)(Address))[0] = ((volatile UCHAR *)(Address))[0];
        ((volatile UCHAR *)(Address))[Size - 1] = ((volatile UCHAR *)(Address))[Size - 1];

#else

        if ((ULONG_PTR) (Address) >= (ULONG_PTR) MM_USER_PROBE_ADDRESS)
        {
            *((volatile UCHAR * const ) MM_USER_PROBE_ADDRESS) = 0;
        }

        *(volatile UCHAR *) (Address) = *(volatile UCHAR *) (Address);
        if (Size > Alignment)
        {
            ((volatile UCHAR *) (Address))[(Size - 1) & ~(SIZE_T) (Alignment - 1)] = ((volatile UCHAR *) (Address))[(Size - 1) & ~(SIZE_T) (Alignment - 1)];
        }

#endif

    }
}



